package com.software.admincenter.controller;

import com.software.admincenter.domain.Result;
import com.software.admincenter.entity.AdminInfo;
import com.software.admincenter.service.AdminInfoService;
import com.software.admincenter.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Author: huanhuan
 * @Date: 2023-06-13 08:41
 **/
@RestController
@CrossOrigin
@RequestMapping("/admin_info")
public class AdminInfoController {
    @Autowired
    private AdminInfoService adminInfoService;

    @GetMapping("/list_page")
    public Page<AdminInfo> findAll(
            @RequestParam(name = "pageNo", required = false, defaultValue = "1") String pageNo,
            @RequestParam(name = "pageSize", required = false, defaultValue = "10") String pageSize
    ){
        Page<AdminInfo> page = adminInfoService.findAll(Integer.valueOf(pageNo)-1, Integer.valueOf(pageSize));
        return page;
    }

    @PostMapping("/addAdminInfo")
    public Result<AdminInfo> addAdminInfo(
            @RequestBody AdminInfo adminInfo){
        AdminInfo save = adminInfoService.addAdminInfo(adminInfo);
        return ResultUtil.success(save);
    }

    @GetMapping(value = "/deleteAdminInfoByAdminId/{adminId}")
    public Result<AdminInfo> deleteAdminInfoByAdminId(@PathVariable String adminId){
        adminInfoService.deleteAdminInfoByAdminId(adminId);
        return ResultUtil.success(null);
    }

    @RequestMapping(value = "/updateAdminInfo")
    public Result<AdminInfo> updateAdminInfo(@RequestBody AdminInfo adminInfo){
        adminInfoService.updateAdminInfo(adminInfo);
        return ResultUtil.success(adminInfo);
    }

    @GetMapping("/findAdminInfosByAdminNameLike")
    public Page<AdminInfo> findAdminInfosByAdminNameLike(
            @RequestParam(name = "adminName") String adminName,
            @RequestParam(name = "pageNo", required = false, defaultValue = "1") String pageNo,
            @RequestParam(name = "pageSize", required = false, defaultValue = "10") String pageSize
    ){
        Page<AdminInfo> page = adminInfoService.findAdminInfosByAdminNameLike(adminName, Integer.valueOf(pageNo)-1, Integer.valueOf(pageSize));
        return page;
    }

    //验证用户名密码是否正确
    @PostMapping("/verifyLogin")
    public Result<AdminInfo> verifyLogin(
            @RequestBody Map<String, String> data){
        System.out.println(data.get("adminName") + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" + data.get("password"));
        List<AdminInfo> adminInfos = adminInfoService.verifyLogin(data.get("adminName"),data.get("password"));
        if(adminInfos.size()>0){
            return ResultUtil.success(adminInfos);
        }else
            return ResultUtil.error("2","用户名密码不匹配",adminInfos);
    }

}
